﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    var ApplicationType = Request.QueryString["type"].ToString();
    var options = new AjaxOptions
    {
        OnBegin = "ajaxBegin",
        OnSuccess = "ajaxSuccess",
        OnFailure = "ajaxError",
        OnComplete = "ajaxComplete"
    };
}

@(
    Html.Kendo().ToolBar()
    .Name("toolbar")
    .Items(item =>
    {
        if (ViewBag.ActionType == "Edit")
        {
            item.Add().Type(CommandType.Button).Text("保存").Click("SaveSubmit");
            item.Add().Type(CommandType.Button).Text("提交").Click("ApplySubmit");
            item.Add().Type(CommandType.Button).Text("作废");
        }
        else if (ViewBag.ActionType == "Applying")
        {
            item.Add().Type(CommandType.Button).Text("撤回");
        }
        else if (ViewBag.ActionType == "Approve")
        {
            item.Add().Type(CommandType.Button).Text("审核");
            item.Add().Type(CommandType.Button).Text("驳回");
        }
        item.Add().Type(CommandType.Button).Text("返回");
    })
)

@using (Ajax.BeginForm("Apply", "ApplicationForm", options, new { id = "AjaxForm" }))
{
    @Html.Hidden("ApplicationType", ApplicationType)
    <table class="apply-table" cellpadding="10" cellspacing="0">
        <thead>
            <tr>
                <td colspan="6" class="thead-title">
                    @ApplicationType
                    <div> @DateTime.Now </div>
                </td>
            </tr>
        </thead>
        @RenderBody()
    </table>

    @Html.ValidationSummary(false)

    // 第一个参数代表约定的节点名
    // 第二个参数代表是否必须
    @RenderSection("ValidateSection", false)
}

<script>

    // 防止重复提交的锁
    var lock = false;

    /**
     * 保存表单
     */
    function SaveSubmit() {
        $("#AjaxForm").attr("action", "/ApplicationForm/Save");
        $("#AjaxForm").submit();
    }

    /**
     * 提交表单
     */
    function ApplySubmit() {
        $("#AjaxForm").attr("action", "/ApplicationForm/Apply");
        $("#AjaxForm").submit();
    }

    /**
     * 表单提交前 1
     * @@param state 状态信息
     * @@param request 请求信息
     */
    function ajaxBegin(state, request) {
        if (lock) { return false; }
        lock = true;
    }

    /**
     * 表单提交成功事件 2
     * @@param response 响应
     */
    function ajaxSuccess(response) {
        if (response.IsSuccess) {
            openNotification(response.Message, "success");
            // 跳转到我的申请记录
            location.href = "/ApplicationForm/MyApplication";
        }
        else {
            openNotification(response.Message, "error");
            lock = false;
        }
    }

    /**
     * 表单提交失败事件 2
     * @@param response 响应
     */
    function ajaxError(response) {
        console.log(response);
        alert(response)
        lock = false;
    }

    /**
     * 表单提交完成时 3
     * @@param state 状态信息
     * @@param message 原因短语
     */
    function ajaxComplete(state, message) {
    }
</script>
